<?php
class dept {

	var $CI;
	var $dept_model;

	public function __construct() {
		$this->CI =& get_instance();
		$this->CI->load->model("common/adm_dept_model");
		$this->dept_model =$this->CI->adm_dept_model;
	}

	public function get_depts($parent_id) {

		return $this->dept_model->query_depts($parent_id);
	}
	public function get_parent_id($dept_id){
		$dept= $this->dept_model->query_parent_id($dept_id);
		if ($dept) {
			return $dept['parent_id'];
		}else{
			return -1;
		}
	}

	public function save_dept($dept){
		$check_result=$this->check_dept($dept);
		if (!$check_result['result']) {
			return $check_result;
		}
		$dept_id =$this->dept_model->insert_dept($dept);
		$result['result']=true;
		$result['redirect_url']=base_url()."index.php/common_dept_manage";
		return $result;
	}

	public function check_dept($dept,$dept_id=""){
		$result['result'] =true;
		$result['message']="区域名称可以使用";
		if ($dept['dept_name']=="") {
			$result['result']=false;
			$result['message']="区域名称不能为空";
			return $result;
		}
		$dept_info =$this->get_dept_info_by_name($dept['dept_name']);
		if ($dept_info=="") {
			$result['result']=true;
			$result['message']="区域名称可以使用";
			return $result;
		}
		if ($dept['parent_id']==$dept_info['parent_id']&&$dept_info['dept_id']!=$dept_id) {
			$result['result']=false;
			$result['message']="相同区域已经存在,请重新输入";
			return $result;
		}
		return $result;
	}

	public function get_dept_info($dept_id){
		return $this->dept_model->query_dept_info($dept_id);
	}

	public function get_dept_info_by_name($dept_name){
		return $this->dept_model->query_dept_info_by_name($dept_name);
	}

	public function set_delete_dept($dept_id){
		$this->dept_model->set_delete_dept($dept_id);

	}
	public function update_dept($dept,$dept_id){
		$check_result=$this->check_dept($dept,$dept_id);
		if (!$check_result['result']) {
			return $check_result;
		}
		$dept_id =$this->dept_model->update_dept($dept,$dept_id);
		$result['result']=true;
		$result['redirect_url']=base_url()."index.php/common_dept_manage";
		return $result;
	}
}
?>